package ios.di.logviewer.ui;

import ios.di.logviewer.data.PageProcessor;

import java.awt.Panel;

import javax.swing.*;


public class PagingPanel extends Panel {

	private MainFrame mainFream;
	private PageProcessor pagePro;

	JButton nextPageButton, prePageButton;
	JLabel pageLabel;

	public PagingPanel(MainFrame mainFream, PageProcessor pagePro) {
		super();
		this.mainFream = mainFream;
		this.pagePro = pagePro;
		this.initialize();
	}

	private void initialize() {
		prePageButton = new JButton("上一页");
		prePageButton.setActionCommand("PrePage");
		prePageButton.addActionListener(this.mainFream);

		pageLabel = new JLabel();

		nextPageButton = new JButton("下一页");
		nextPageButton.setActionCommand("NextPage");
		nextPageButton.addActionListener(this.mainFream);

		this.checkStatus();

		this.add(prePageButton);
		this.add(pageLabel);
		this.add(nextPageButton);
	}

	private void checkStatus() {
		if (pagePro == null) {
			prePageButton.setEnabled(false);
			nextPageButton.setEnabled(false);
			pageLabel.setText("第0页");
		} else {
			if (pagePro.getPageIndex()==0)
				prePageButton.setEnabled(false);
			else 
				prePageButton.setEnabled(true);
			if (!pagePro.hasNext())
				nextPageButton.setEnabled(false);
			else 
				nextPageButton.setEnabled(true);
			pageLabel.setText("第" + (pagePro.getPageIndex()+1)  + "页");
		}
	}

	public void setPagePro(PageProcessor pagePro) {
		this.pagePro = pagePro;
	}
	public void fireUpdate()
	{
		this.checkStatus();
		this.repaint();
	}
}
